package com.yq.cp.controller.yw;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.yw.YwGdxx;
import com.yq.cp.entity.yw.dto.YwGdxxDochuDto;
import com.yq.cp.entity.yw.vo.YwGdxxVo;
import com.yq.cp.service.yw.YwGdxxService;
import com.yq.cp.utils.ExcelUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运维-工单信息 前端控制器
 * </p>
 *
 * @author zh
 * @since 2024-09-07
 */
@RestController
@RequestMapping("/yw-gdxx")
public class YwGdxxController {

    @Resource
    YwGdxxService ywGdxxService;


    @ResponseBody
    @PostMapping("add")
    public ApiResult add(@RequestBody YwGdxx ywGdxx) {
        ywGdxxService.add(ywGdxx);
        return ApiResult.ok();
    }
    @ResponseBody
    @PostMapping("sc")
    public ApiResult sc(@RequestBody YwGdxx ywGdxx) {
        ywGdxxService.sc(ywGdxx);
        return ApiResult.ok();
    }

    @ResponseBody
    @PostMapping("audit1")
    public ApiResult audit1(@RequestBody YwGdxx ywGdxx) {
        ywGdxxService.audit1(ywGdxx);
        return ApiResult.ok();
    }

    @ResponseBody
    @PostMapping("audit2")
    public ApiResult audit2(@RequestBody YwGdxx ywGdxx) {
        ywGdxxService.audit2(ywGdxx);
        return ApiResult.ok();
    }

    @ResponseBody
    @PostMapping("page")
    public ApiResult page(@RequestBody YwGdxxVo vo) {
        IPage<YwGdxx> page = ywGdxxService.page(vo);
        return ApiResult.ok().data(page);
    }
    @ResponseBody
    @PostMapping("page/daochu")
    public ApiResult pageDaochu(@RequestBody YwGdxxVo vo) {
        List<YwGdxxDochuDto> daochu = ywGdxxService.daochu(vo);

        ExcelUtil.doWrite("工单信息","工单信息",YwGdxxDochuDto.class,daochu);
        return ApiResult.ok();
    }
    @ResponseBody
    @PostMapping("get")
    public ApiResult get(@RequestBody YwGdxxVo vo) {
        YwGdxx ywGdxx = ywGdxxService.get(vo.getId());
        return ApiResult.ok().data(ywGdxx);
    }
    @ResponseBody
    @PostMapping("get/daochu")
    public ApiResult getDaochu(@RequestBody YwGdxxVo vo, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        YwGdxx ywGdxx = ywGdxxService.get(vo.getId());
        // 防止日志记录获取session异常
        request.getSession();
        // 设置编码格式
        response.setContentType("application/pdf;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("工单详情", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".pdf");
        ywGdxxService.getDaochu(ywGdxx, response);
        return ApiResult.ok();
    }
    @ResponseBody
    @PostMapping("sl")
    public ApiResult sl(@RequestBody YwGdxx ywGdxx) {
        ywGdxxService.sl(ywGdxx);
        return ApiResult.ok();
    }

    @ResponseBody
    @PostMapping("yj")
    public ApiResult yj(@RequestBody YwGdxx ywGdxx) {
        ywGdxxService.yj(ywGdxx);
        return ApiResult.ok();
    }

    @ResponseBody
    @PostMapping("ccggry")
    public ApiResult ccggry(@RequestBody YwGdxx ywGdxx) {
        List<Map<String, Object>> ccggry = ywGdxxService.ccggry1(ywGdxx.getId());
        return ApiResult.ok().data(ccggry);
    }

    @ResponseBody
    @PostMapping("ccggry2")
    public ApiResult ccggry2(@RequestBody YwGdxx ywGdxx) {
        List<Map<String, Object>> ccggry = ywGdxxService.ccggry2(ywGdxx.getId());
        return ApiResult.ok().data(ccggry);
    }

    /**
     * 巡检任务问题上报
     * @param ywGdxx
     * @return
     */
    @ResponseBody
    @PostMapping("addQuestion")
    public ApiResult addQuestion(@RequestBody YwGdxx ywGdxx) {
        ywGdxxService.addQuestion(ywGdxx);
        return ApiResult.ok();
    }
}

